﻿using System;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FlexCell;
using YArchitech.LIB;
using YJKCommonTableTool;
using YJKStatisticTable.AreaTable.DataManager;
using YJKStatisticTable.AreaTable.Forms;

namespace YJKStatisticTable.AreaTable
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdNetAreaStatistic : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			NetAreaStatisticalForm netAreaStatisticalForm = new NetAreaStatisticalForm();
			DialogResult dialogResult = netAreaStatisticalForm.ShowDialog();
			FlexCell.Grid grid = new FlexCell.Grid();
			if (dialogResult != DialogResult.Cancel)
			{
				string text = Path.Combine(Product.InstallLocation, "Templates", "NetAreaTempletlFile");
				text = text + "\\" + netAreaStatisticalForm.cmbGrid.Text + ".hymb";
				if (!grid.LoadFromXML(text))
				{
					return Autodesk.Revit.UI.Result.Failed;
				}
				if (netAreaStatisticalForm.cmbGrid.Text == "防火分区面积表")
				{
					FireCompartmentAreaInfo fireCompartmentAreaInfo = new FireCompartmentAreaInfo(document, ref grid, netAreaStatisticalForm.m_gridSet);
					if (!fireCompartmentAreaInfo.GetStatisticGrid())
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
				}
				else if (netAreaStatisticalForm.cmbGrid.Text == "主要经济技术指标表")
				{
					TechnoEconomicIndicatorInfo technoEconomicIndicatorInfo = new TechnoEconomicIndicatorInfo(document, ref grid, netAreaStatisticalForm.m_gridSet);
					if (!technoEconomicIndicatorInfo.GetStatisticGrid())
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
				}
				else if (netAreaStatisticalForm.cmbGrid.Text == "建筑物明细表")
				{
					BuildingDetailList buildingDetailList = new BuildingDetailList(document, ref grid, netAreaStatisticalForm.m_gridSet);
					if (!buildingDetailList.GetStatisticGrid())
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
				}
				if (dialogResult == DialogResult.OK)
				{
					CREATENEWVIEW newView;
					if (netAreaStatisticalForm.m_gridSet.NewView)
					{
						newView = CREATENEWVIEW.NEWVIEW;
					}
					else
					{
						newView = CREATENEWVIEW.EXIST;
					}
					GridToolExportImportRevit gridToolExportImportRevit = new GridToolExportImportRevit(commandData, grid, netAreaStatisticalForm.m_gridSet.GridName, netAreaStatisticalForm.m_gridSet.LineWidthOut, netAreaStatisticalForm.m_gridSet.LineWidthInner, newView);
					gridToolExportImportRevit.DrawInRevitView();
				}
				else if (dialogResult == DialogResult.Yes)
				{
					switch (netAreaStatisticalForm.m_eFileType)
					{
					case 1:
					{
						GridToolExportImportExcel gridToolExportImportExcel = new GridToolExportImportExcel(false);
						gridToolExportImportExcel.ExportExcel(grid, netAreaStatisticalForm.m_strFilePath);
						break;
					}
					case 2:
					{
						GridToolExportImportExcel gridToolExportImportExcel2 = new GridToolExportImportExcel(true);
						gridToolExportImportExcel2.ExportExcel(grid, netAreaStatisticalForm.m_strFilePath);
						break;
					}
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
